package com.ozo.boot.manage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ozo.boot.common.log.annotation.OperateLog;
import com.ozo.boot.manage.pojo.vo.SysPermissionTreeVO;
import com.ozo.boot.manage.pojo.vo.SysPermissionVO;
import com.ozo.boot.common.base.model.BaseBatchDTO;
import com.ozo.boot.common.base.model.ResultVO;
import com.ozo.boot.manage.pojo.dto.ChangeStatusDTO;
import com.ozo.boot.manage.pojo.dto.SysPermissionQueryDTO;
import com.ozo.boot.manage.pojo.dto.SysPermissionSaveDTO;
import com.ozo.boot.manage.service.ISysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@Tag(name = "权限管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/permission")
public class SysPermissionController {

    private final ISysPermissionService sysPermissionService;

    @OperateLog(value = "添加权限", module = "权限")
    @SaCheckPermission("manage:permission:add")
    @Operation(summary = "添加权限")
    @PostMapping("/add")
    public ResultVO<Void> add(@RequestBody @Valid SysPermissionSaveDTO saveDTO) {
        sysPermissionService.add(saveDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "删除权限", module = "权限")
    @SaCheckPermission("manage:permission:remove")
    @Operation(summary = "删除权限")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysPermissionService.remove(batchDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "编辑权限", module = "权限")
    @SaCheckPermission("manage:permission:edit")
    @Operation(summary = "编辑权限")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Valid SysPermissionSaveDTO saveDTO) {
        sysPermissionService.edit(saveDTO);
        return ResultVO.success();
    }

    @SaCheckPermission("manage:permission:list")
    @Operation(summary = "权限列表")
    @GetMapping("/list")
    public ResultVO<List<SysPermissionVO>> list(SysPermissionQueryDTO queryDTO) {
        return ResultVO.success(sysPermissionService.list(queryDTO));
    }

    @OperateLog(value = "修改状态", module = "权限")
    @SaCheckPermission("manage:permission:changeStatus")
    @Operation(summary = "修改状态")
    @PutMapping("/changeStatus")
    public ResultVO<Void> changeStatus(@RequestBody @Valid ChangeStatusDTO statusDTO) {
        sysPermissionService.changeStatus(statusDTO);
        return ResultVO.success();
    }

    @Operation(summary = "当前登录人权限")
    @GetMapping("/oneself")
    public ResultVO<List<SysPermissionTreeVO>> oneself() {
        return ResultVO.success(sysPermissionService.oneself());
    }

}
